<template>
	<view class="bankAccount">
		<view class="tip">
			请绑定入驻经营主体名下的银行账户，用于收款。
		</view>
		<view class="form-wrapper">
			<view class="item">
				<view class="label">
					主体类型
				</view>
				<view class="value">
					<text v-if="subjectType == 1">自然人</text>
					<text v-else-if="subjectType == 2">企业</text>
					<text v-else-if="subjectType == 3">个体户</text>
				</view>
				<uni-icons type="right" size="18" color="#ffffff"></uni-icons>
			</view>
			<view class="item">
				<view class="label">
					开户名
				</view>
				<view class="value">
					<input type="text" v-model="form.accountName" maxlength="20" placeholder="-" disabled class="input">
				</view>
				<uni-icons type="right" size="18" color="#ffffff"></uni-icons>
			</view>
			<view class="item">
				<view class="label">
					银行账号
				</view>
				<view class="value">
					<input type="number" v-model="form.account" placeholder="请输入银行账号" class="input">
				</view>
				<uni-icons @tap="selectUploadMethod" type="camera" size="28" color="#555666"></uni-icons>
			</view>
			<view class="item">
				<view class="label">
					开户银行
				</view>
				<view class="value" @click="openBank">
					<text v-if="!form.depositBank" class="placeholder">请选择开户银行</text>
					<text v-else class="input">{{ form.depositBankName }}</text>
				</view>
				<uni-icons type="right" size="18" color="#898989"></uni-icons>
			</view>
			<view class="item" v-if="subjectType == 3">
				<view class="label">
					账户类型
				</view>
				<view class="value" @click="selectType">
					<text v-if="!form.accountType" class="placeholder">请选择账户类型</text>
					<text v-else-if="form.accountType === 1" class="input">个人账户</text>
					<text v-else-if="form.accountType === 2" class="input">对公账户</text>
				</view>
				<uni-icons type="right" size="18" color="#898989"></uni-icons>
			</view>
			<view class="item">
				<view class="label">
					手机号码
				</view>
				<view class="value">
					<input type="text" v-model="form.phone" maxlength="20" placeholder="银行开户时预留的手机号码" class="input">
				</view>
				<uni-icons type="right" size="18" color="#ffffff"></uni-icons>
			</view>
		</view>

		<view class="btn" @click="submit">
			确认
		</view>

		<!-- <uni-city :show="addressShow" :default="[address.provinceCode,address.cityCode]"
			@change="selectCity"></uni-city> -->
	</view>
</template>

<script>
	// import uniCity from '@/components/wx-uni-city/index.vue'
	import {
		pathToBase64
	} from 'image-tools'

	export default {
		data() {
			return {
				form: {
					account: '',
					accountName: '',
					depositBank: '',
					depositBankName: '',
					accountType: '',
					phone: ''
				},
				bankList: [],
				subjectType: '',
			}
		},
		onLoad(option) {
			this.subjectType = option.type;
			const info = JSON.parse(option.info);
			this.form = {
				supplierId: info.supplierId,
				id: info.id,
				account: info.account,
				accountName: info.accountName,
				depositBank: info.depositBank,
				depositBankName: info.depositBankName,
				accountType: info.accountType,
				phone: info.phone,
			}
			let supplierInfo_register = uni.getStorageSync('supplierInfo_register')
			if (option.type == 1) {
				this.form.accountType = 1;
				this.form.accountName = supplierInfo_register.supplierAdmin.realName
			} else if (option.type == 2) {
				this.form.accountType = 2;
				this.form.accountName = supplierInfo_register.supplierSubject.subjectName
			}
			this.getBankList();
		},
		computed: {

		},
		methods: {
			async getBankList() {
				const data = await this.$api.post('/adm/bank/listNoPage')
				if (data.code === 200) {
					this.bankList = data.data;
					console.log(this.bankList)
				}
			},
			selectUploadMethod() {
				const _this = this;
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择
					success: async function(res) { //idCard //bankCard //businessLicense
						_this.upload(res.tempFilePaths[0])
					}
				});
			},
			async upload(path) {
				const _this = this;
				//const url = await _this.$uploadFn(path, 0, 'bankCard', 0, true);
				const base64 = await pathToBase64(path);
				plus.io.resolveLocalFileSystemURL(path, function(entry) {
					entry.remove(function(e) {
						console.log("删除成功");
					}, function(e) {
						console.log("删除失败");
					});
				});
				const data = await this.$api.post('/adm/ocr', {
					ocrImgData: base64.replace('data:image/jpeg;base64,', ''),
					ocrType: 2
				})
				if (data.code === 200) {
					const info = JSON.parse(data.data)
					this.form.account = info.CardNo;
					this.form.depositBank = '';
					this.form.depositBankName = '';
					let bankName = info.BankInfo;
					let bank = {};
					if (bankName.indexOf('(') > -1) {
						bankName = bankName.split('(')[0]
						bank = this.bankList.find(item => item.bankName.indexOf(bankName) > -1)
						this.form.depositBank = bank.bankCode;
						this.form.depositBankName = bank.bankName;
					}

				} else {
					uni.showToast({
						title: data.msg,
						icon: 'none',
						duration: 2500
					})
				}
			},
			openBank() {
				uni.navigateTo({
					url: '/subPage_supplier/register/searchBank'
				})
			},
			setBank(item, type) {
				console.log(item)
				this.form.depositBank = item.bankCode;
				this.form.depositBankName = item.bankName;
			},
			selectType() {
				uni.showActionSheet({
					itemList: ['个人账户', '对公账户'],
					success: (e) => {
						this.form.accountType = e.tapIndex + 1;
						let supplierInfo_register = uni.getStorageSync('supplierInfo_register')
						if (this.form.accountType == 1) {
							this.form.accountName = supplierInfo_register.supplierLegal.realName
						} else if (this.form.accountType == 2) {
							this.form.accountName = supplierInfo_register.supplierSubject.subjectName
						}
					}
				})
			},
			async submit() {
				if (!this.form.accountName) {
					uni.showToast({
						title: '请输入开户名称',
						icon: 'none'
					})
					return
				}
				if (!this.form.account) {
					uni.showToast({
						title: '请输入银行账号',
						icon: 'none'
					})
					return
				}
				if (!this.form.depositBankName) {
					uni.showToast({
						title: '请选择开户银行',
						icon: 'none'
					})
					return
				}
				if (!this.form.accountType) {
					uni.showToast({
						title: '请选择账户类型',
						icon: 'none'
					})
					return
				}
				if (this.subjectType == 1 && !this.form.phone) {
					uni.showToast({
						title: '请输入银行开户时预留的手机号码',
						icon: 'none',
						duration: 2000
					})
					return
				}

				const infoData = {
					...this.form
				}
				if (this.form.supplierId) {
					if (this.form.id) {
						await this.$api.put('/adm/supplier-bank', infoData)
					} else {
						await this.$api.post('/adm/supplier-bank', infoData)
					}
				}
				var pages = getCurrentPages();
				var prevPage = pages[pages.length - 2];
				if (prevPage == undefined || prevPage == null) return;
				prevPage.$vm.setBankData(infoData);
				uni.navigateBack()
			}
		},
		components: {
			//uniCity
		}
	}
</script>

<style lang="scss" scoped>
	.bankAccount {
		.tip {
			height: 60upx;
			line-height: 60upx;
			background-color: #ffffff;
			color: #FF8D1A;
			padding: 0 20upx;
			font-size: 28upx;
			margin-top: 1upx;
		}

		.form-wrapper {
			margin: 20upx 20upx;
			border-radius: 20upx;
			background-color: #ffffff;

			.item {
				min-height: 108upx;
				display: flex;
				align-items: center;
				padding: 0 20upx;
				border-top: 2upx solid #efefef;

				&:first-child {
					border-top: 0upx solid #efefef;
				}

				.label {
					color: #222222;
					font-size: 32upx;
				}

				.value {
					flex: 1;
					display: flex;
					align-items: center;
					justify-content: flex-end;

					.input {
						padding-left: 40upx;
						width: 100%;
						text-align: right;
					}

					.textarea {
						width: 500upx;
						height: 70upx;
						margin: 20upx 0;
						text-align: right;
					}

					.placeholder {
						color: #7F7F7F;
					}

					.checkbox-item {
						display: flex;
						align-items: center;
						margin-left: 40upx;
					}
				}
			}
		}

		.bank-popup-wrapper {
			min-height: 900upx;
			background-color: #ffffff;
			border-radius: 20upx 20upx 0 0;
			position: relative;
			bottom: calc(0upx - env(safe-area-inset-bottom));

			.title-wrapper {
				height: 88upx;
				background-color: #f5f5f5;
				display: flex;
				align-items: center;
				padding: 0 32upx;
				border-radius: 20upx 20upx 0 0;
				justify-content: space-between;
			}

			.item {
				height: 88upx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 0 32upx;
				border-bottom: 2upx solid #efefef;

				.label {
					color: #666666;
				}

				.value {
					color: #343434;
				}
			}
		}

		.btn {
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100%;
			height: 98upx;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #ffffff;
			background-color: #555BFF;
			transition: .1s linear;

			&:active {
				background-color: #555BFFDC;
			}
		}
	}
</style>